﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Text;

namespace V.WP.PM_Thesis
{
    public class MyZip
    {
        /// <summary>
        /// UnZip
        /// </summary>
        /// <param name="filePath">filePath</param>
        public static void UnZip(string filePath)
        {
            #region MyRegion

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string dicName = "html";
                using (ZipInputStream zipInputStream = new ZipInputStream(store.OpenFile(filePath, FileMode.Open)))
                {
                    ZipEntry zipEntry;
                    byte[] data = new byte[2048];
                    int size = 2048;
                    while ((zipEntry = zipInputStream.GetNextEntry()) != null)
                    {
                        if (zipEntry != null)
                        {
                            if (zipEntry.IsFile)
                            {
                                string path = dicName;
                                IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
                                string fName = zipEntry.Name;
                                //string fName = Encoding.UTF8.GetString(Encoding.Unicode.GetBytes(zipEntry.Name), 0, zipEntry.Name.Length);
                                #region MyRegion
                                if (fName.Contains("/"))
                                {
                                    string[] nameSplit = fName.Split('/');
                                    if (!path.Contains(nameSplit[0]))
                                    {
                                        path += "/" + nameSplit[0];
                                    }
                                }
                                else
                                {
                                    path = dicName;
                                }
                                #endregion

                                if (!Directory.Exists(path))
                                {
                                    isf.CreateDirectory(path);
                                }
                                using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(System.IO.Path.Combine(path + "\\", fName.Substring(fName.LastIndexOf("/") + 1, fName.Length - fName.LastIndexOf("/") - 1)), FileMode.OpenOrCreate, isf))
                                {
                                    while (true)
                                    {
                                        size = zipInputStream.Read(data, 0, data.Length);
                                        if (size <= 0)
                                            break;
                                        fileStream.Write(data, 0, size);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion
        }
    }
}
